/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tc.maas.bulkfilerenamer.utils.texts;

import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import tc.maas.bulkfilerenamer.utils.exceptions.LanguageException;
import tc.maas.bulkfilerenamer.utils.settings.Settings;

/**
 *
 * @author michel
 */
public enum ErrorMessages {

    NO_NAMING_CONVENTION_FOUND_ERROR(),
    NOT_A_FILE_ERROR();
    private String errorMessage;
    private static final String TAG_ID = ErrorMessages.class.getSimpleName();

    private ErrorMessages() {
        try {
            getMessageFromLanguageFile();
        } catch (LanguageException ex) {
            Logger.getLogger(ErrorMessages.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void getMessageFromLanguageFile() throws LanguageException {
        InputStream languageFile = this.getClass().getResourceAsStream("/Language_File_" + Settings.getSettings().getCurrentLanguage().getCode() + ".xml");
        Document languageDocument = null;
        try {
            languageDocument = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder().parse(languageFile);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(ErrorMessages.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(ErrorMessages.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ErrorMessages.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (languageDocument == null) {
            throw new LanguageException();
        }
        Element errorMessagesTag = languageDocument.getElementById(TAG_ID);
        Element errorMessageTag = languageDocument.getElementById(this.name()).getParentNode().equals(errorMessagesTag) ? languageDocument.getElementById(this.name()) : null;
        if (errorMessageTag == null) {
            throw new LanguageException();
        }
        errorMessage = errorMessageTag.getTextContent();
    }

    public String getMessage(){
        return errorMessage;
    }
}
